package controller;


import enums.SysInfoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.CourseM;
import service.AddCourseService;
import utils.ResultUtils;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    AddCourseService addCourseService;


    @RequestMapping(value = "/index")
    public String index(){
        return "/WEB-INF/view/courseM/courseM.jsp";
    }

    @RequestMapping(value = "/get",method = RequestMethod.GET)
    @ResponseBody
    public List<CourseM> getAll(@RequestParam("name") String name){
        List<CourseM> listCourse = addCourseService.getAll(name);
        System.out.println(listCourse);
        return listCourse;
    }

    @RequestMapping(value = "/toadd")
    public String toadd(){
        return "/WEB-INF/view/courseM/add.jsp";
    }

    @RequestMapping(value = "/addcourse",method = RequestMethod.POST)
    @ResponseBody
    public String addcourse(@Valid @RequestBody CourseM courseM,HttpServletRequest request){
        try{
            addCourseService.addCourse(courseM);
        }catch (Exception e){
            request.setAttribute("result","增加课程出错错误");
            e.printStackTrace();
            return SysInfoEnum.ERROR.getMessage();
        }

        return SysInfoEnum.SUCCESS.getMessage();
    }

    @RequestMapping(value = "/delete")
    public String delete(String id){
        addCourseService.deleteCourse(id);
        return "redirect:/demo/course/index";
    }

    @RequestMapping(value = "/getByArray")
    @ResponseBody
    public List<CourseM> getByArray(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                         @RequestParam(value = "size",defaultValue = "4") Integer size){
        List<CourseM> courseMS =  addCourseService.getCourseByArray(currentPage, size);
//        map.put("number",userFeedbackDTOPage.getTotalElements());
        return courseMS;
    }


    @RequestMapping(value = "/getNumber",method = RequestMethod.POST)
    @ResponseBody
    public Integer getNumber(){
        List<CourseM> courseMS = addCourseService.getAllByArray();
        return courseMS.size();
    }

}
